package leetcode.code;

import leetcode.IDebug;

public class Solution8 implements IDebug {

	public int myAtoi(String s) {
		char[] chars = s.toCharArray();
		Long num = null;
		Boolean negative = null;
		for (char c : chars) {
			if (c == ' ') {
				if (num == null && negative == null)
					continue;
				else
					break;
			}
			if (c == '-') {
				if (negative == null && num == null) {
					negative = true;
					continue;
				} else {
					break;
				}
			}
			if (c == '+') {
				if (negative == null && num == null) {
					negative = false;
					continue;
				} else {
					break;
				}
			}
			if (c < '0' || c > '9') {
				break;
			}
			if (num == null) {
				num = 0l;
			}
			num = num * 10 + c - '0';
			if (num >= Integer.MAX_VALUE)
				break;
		}
		if (num == null) {
			num = 0l;
		}
		long n = negative == null ? num : negative ? -num : num;
		int ans = n >= Integer.MAX_VALUE ? Integer.MAX_VALUE : n <= Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) n;
		return ans;
	}

	@Override
	public void debug1() {
		String s = "9223372036854775808";
		int r = this.myAtoi(s);
		System.out.println(r);

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution8 so = new Solution8();
		so.debug1();

	}

}
